import { ExamAnswerMode, ExamMatchRule, QuestionType, TemplateSchema, TemplateVO } from '@/api/exam/template/types';

// 定义题型处理策略对象
export const questionTypeStrategies = {
  [QuestionType.Radio]: {
    icon: 'radio-hollow',
    inputType: 'text',
    modelPath: 'title',
    answerModes: [
      { label: '此问题有唯一答案', value: 'onlyOne' },
      { label: '每个选项有对应分值', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    showCorrectAnswer: (question, item) =>
      item.attribute.examCorrectAnswer === item.id &&
      ![ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode),
    showScore: (question, item) =>
      !!item.attribute.examCorrectAnswer &&
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode)
  },
  [QuestionType.TrueFalse]: {
    icon: 'radio-hollow',
    inputType: 'text',
    modelPath: 'title',
    answerModes: [
      { label: '此问题有唯一答案', value: 'onlyOne' }
    ],
    showCorrectAnswer: (question, item) =>
      item.attribute.examCorrectAnswer === item.id &&
      ![ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode),
    showScore: (question, item) =>
      !!item.attribute.examCorrectAnswer &&
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode)
  },
  [QuestionType.Checkbox]: {
    icon: 'checkbox-hollow',
    inputType: 'text',
    modelPath: 'title',
    answerModes: [
      { label: '全部答对才得分', value: 'selectAll' },
      { label: '答对几项得几分，答错不得分', value: 'selectCorrect' },
      { label: '每个选项有对应分值', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    showCorrectAnswer: (question, item) =>
      item.attribute.examCorrectAnswer === item.id &&
      ![ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode),
    showScore: (question, item) =>
      !!item.attribute.examCorrectAnswer &&
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode)
  },
  [QuestionType.FillBlank]: {
    inputType: 'text',
    inputTitle: true,
    modelPath: 'attribute.examCorrectAnswer',
    answerModes: [
      { label: '全部答对才得分', value: 'selectAll' },
      { label: '答对几项得几分', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    matchRules: [
      { label: '回答与答案完全相同得分', value: 'completeSame' },
      { label: '回答中包含答案即可得分', value: 'contain' }
    ],
    showCorrectAnswer: () => false,
    showScore: (question) =>
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode)
  },
  [QuestionType.MultipleBlank]: {
    inputType: 'text',
    inputTitle: true,
    modelPath: 'attribute.examCorrectAnswer',
    answerModes: [
      { label: '全部答对才得分', value: 'selectAll' },
      { label: '答对几项得几分', value: 'select' },
      { label: '人工评分', value: 'manual' }
    ],
    matchRules: [
      { label: '回答与答案完全相同得分', value: 'completeSame' },
      { label: '回答中包含答案即可得分', value: 'contain' }
    ],
    showCorrectAnswer: () => false,
    showScore: (question) =>
      [ExamAnswerMode.select, ExamAnswerMode.selectCorrect].includes(question.template.attribute.examAnswerMode)
  },
  [QuestionType.Textarea]: {
    inputType: 'textarea',
    inputResize: 'none',
    modelPath: 'attribute.examCorrectAnswer',
    answerModes: [
      { label: 'AI打分', value: 'ai' },
      { label: '人工评分', value: 'manual' }
    ],
    showCorrectAnswer: () => false,
    showScore: () => false
  }
};

let generatedId = generateRandomId();
export const questionTypeList = ref<any | TemplateVO[]>([
  {
    templateId: null,
    repoId: null,
    questionType: QuestionType.Radio,
    orderNum: 1,
    template: {
      id: null,
      title: '请选择一个选项',
      type: QuestionType.Radio,
      description: null,
      orderNum: 1,
      attribute: {
        examScore: 5,
        examAnswerMode: ExamAnswerMode.onlyOne,
        examAnalysis: ''
      },
      children: [
        {
          id: generatedId,
          title: '选项1',
          attribute: {
            examCorrectAnswer: generatedId
          }
        },
        {
          id: generateRandomId(),
          title: '选项2',
          attribute: {
            examCorrectAnswer: null
          }
        },
        {
          id: generateRandomId(),
          title: '选项3',
          attribute: {
            examCorrectAnswer: null
          }
        },
        {
          id: generateRandomId(),
          title: '选项4',
          attribute: {
            examCorrectAnswer: null
          }
        }
      ],
      tags: [],
    }

  },
  {
    templateId: null,
    repoId: null,
    questionType: QuestionType.Checkbox,
    orderNum: 2,
    template: {
      id: null,
      title: '请选择以下选项 (多选)',
      type: QuestionType.Checkbox,
      orderNum: 2,
      description: null,
      attribute: {
        examScore: 5,
        examAnswerMode: ExamAnswerMode.selectAll,
        examAnalysis: ''
      },
      children: [
        {
          id: generatedId,
          title: '选项1',
          attribute: {
            examCorrectAnswer: generatedId
          }
        },
        {
          id: generateRandomId(),
          title: '选项2',
          attribute: {
            examCorrectAnswer: null
          }
        },
        {
          id: generateRandomId(),
          title: '选项3',
          attribute: {
            examCorrectAnswer: null
          }
        },
        {
          id: generateRandomId(),
          title: '选项4',
          attribute: {
            examCorrectAnswer: null
          }
        }
      ],
      tags: []
    }
  },
  {
    templateId: null,
    repoId: null,
    questionType: QuestionType.TrueFalse,
    orderNum: 3,
    template: {
      id: null,
      title: '请判断本项叙述是否正确？',
      type: QuestionType.TrueFalse,
      orderNum: 3,
      description: null,
      attribute: {
        examScore: 5,
        examAnswerMode: ExamAnswerMode.onlyOne,
        examAnalysis: ''
      },
      children: [
        {
          id: generatedId,
          title: '正确',
          attribute: {
            examCorrectAnswer: generatedId
          }
        },
        {
          id: generateRandomId(),
          title: '错误',
          attribute: {
            examCorrectAnswer: null
          }
        }
      ],
      tags: []
    }
  },
  {
    templateId: null,
    repoId: null,
    questionType: QuestionType.FillBlank,
    orderNum: 4,
    template: {
      id: null,
      title: '请填写以下内容',
      type: QuestionType.FillBlank,
      orderNum: 4,
      description: null,
      attribute: {
        examScore: 10,
        examAnswerMode: ExamAnswerMode.select,
        examMatchRule: ExamMatchRule.completeSame,
        examAnalysis: ''
      },
      children: [
        {
          id: generateRandomId(),
          title: '填空1',
          attribute: {
            examScore: 5,
            examCorrectAnswer: null
          }
        }
      ],
      tags: []
    }
  },
  {
    templateId: null,
    repoId: null,
    questionType: QuestionType.MultipleBlank,
    orderNum: 4,
    template: {
      id: null,
      title: '请填写以下内容',
      type: QuestionType.MultipleBlank,
      orderNum: 4,
      description: null,
      attribute: {
        examScore: 10,
        examAnswerMode: ExamAnswerMode.select,
        examMatchRule: ExamMatchRule.completeSame,
        examAnalysis: ''
      },
      children: [
        {
          id: generateRandomId(),
          title: '填空1',
          attribute: {
            examScore: 5,
            examCorrectAnswer: null
          }
        },
        {
          id: generateRandomId(),
          title: '填空2',
          attribute: {
            examScore: 5,
            examCorrectAnswer: null
          }
        }
      ],
      tags: []
    }
  },
  {
    templateId: null,
    repoId: null,
    questionType: QuestionType.Textarea,
    orderNum: 5,
    template: {
      id: null,
      title: '简答题',
      type: QuestionType.Textarea,
      orderNum: 5,
      description: null,
      attribute: {
        examScore: 5,
        examAnswerMode: ExamAnswerMode.ai,
        examAnalysis: ''
      },
      children: [
        {
          id: generateRandomId(),
          title: '填空1',
          attribute: {
            examCorrectAnswer: null
          }
        }
      ],
      tags: []
    }
  }
]);

// 题目类型配置
export const QUESTION_TYPE_CONFIG = {
  Radio: { icon: 'radio', label: '单选题' },
  Checkbox: { icon: 'checkbox', label: '多选题' },
  TrueFalse: { icon: 'true-false', label: '判断题' },
  FillBlank: { icon: 'fill-blank', label: '填空题' },
  MultipleBlank: { icon: 'multiple-blank', label: '多项填空' },
  Textarea: { icon: 'textarea', label: '简答题' }
};


// 生成四位随机字符的函数
export function generateRandomId() {
  const characters = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`;
  let result = '';
  for (let i = 0; i < 4; i++) {
    result += characters.charAt(Math.floor(Math.random() * characters.length));
  }
  return result;
}
